<template>
  <div class="userList">
    <h2>用户管理</h2>
    <el-table
        v-loading="loading"
        :data="userList"
        border
        style="width: 100%">
      <el-table-column
          prop="userCode"
          label="用户编码">
        <template slot-scope="scope">
          <img width="150px" :src="scope.row.idPicPath | showImg"/> <span style="margin-left: 10px">{{
            scope.row.userCode
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
          prop="userName"
          label="用户名">
      </el-table-column>
      <el-table-column
          prop="gender"
          label="性别">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.gender == 1 ? '男' : '女' }}</span>
        </template>
      </el-table-column>
      <el-table-column
          prop="birthday"
          label="出生日期">
      </el-table-column>
      <el-table-column
          prop="phone"
          label="电话号码">
      </el-table-column>
      <el-table-column
          label="操作">
        <template slot-scope="scope">
          <el-button @click="toEdit(scope.row.id)" type="text" size="small">编辑</el-button>
          <el-button @click="delUser(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        background
        layout="prev, pager, next"
        @current-change="toPage"
        :current-page="page.current"
        :page-count="page.pages"
        :page-size="page.size"
        :total="page.total">
    </el-pagination>
  </div>
</template>

<script>
import {userList,userDelById,imgServerUrl} from "@/api"

export default {
  name: "UserList",
  data() {
    return {
      loading: true, //加载中
      user: {},
      userList: [],
      page: {}
    }
  },
  filters:{
    showImg(val){
      return imgServerUrl(val);
    }
  },
  methods: {
    toEdit(id) {
      this.$router.push({path: '/useredit/' + id})
    },
    toPage(pageno) {
      let that = this;
      that.loading = true;
      userList({"pageno":pageno})
          .then(res => { // 成功的回调
            that.page = res.data.data;
            that.userList = res.data.data.records;
            that.loading = false;
          })
          .catch(err => { //失败的回调
            console.log(err)
          })
    },
    delUser(id) {
      let that = this;
      that.$confirm('确定要删除用户吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //确定
        userDelById({"id":id})
            .then(res => {
              if (res.data) {
                that.$message({
                  message: '删除成功',
                  type: 'success'
                });
                that.toPage(that.page.current);
              } else {
                that.$message("删除失败");
              }
            })
            .catch(err => {
              that.$message("操作失败");
            });
      }).catch(() => {

      });
    }
  },
  mounted() {
    this.toPage(1);
  }
}
</script>

<style scoped>
.userList {
}
</style>